<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
</head>

<body ng-app="app">
	<script src="angular-1.5.8/angular.min.js"></script>
	<script>
		function getData($timeout,$q){
			var defer=$q.defer();
			$timeout(function(){
				if(Math.round(Math.random())){
					defer.solve('data received!')
				}else{
					defer.reject('oh no an error! try again');  
				}
			},2000)
			return defer.promise;
		}
		angular.module('app', [])
			.factory('getData', getData)//把getData方法进入factory进行实例化，方便进去run方法进行配置
			.run(function(getData) {//run方法用于快速配置，注入完成后启动（只能是实例化的服务或者参数可以配置）
				var promise = getData();
				promise.then(function(res){
					console.log(res)
				});
			})
		</script>
	</body>

	</html>